1040A - Palindrome Dance - CodeForces Solution


greedy *1000

Please click on ads to support us..

Python Code:

n, a, b = map(int, input().split())
l = list(map(int, input().split()))
sum = 0
costs = {0: a, 1: b}

for i in range(len(l) // 2 if len(l) % 2 == 0 else (len(l) // 2) + 1):
    if l[i] == l[-(i+1)] and l[i] != 2:
        continue
    elif l[i] == l[-(i+1)] and l[i] == 2 and ( i == len(l) - (i+1) ):
        sum += min(a, b)
    elif l[i] == l[-(i+1)] and l[i] == 2:
        sum += 2*min(a, b)
    elif l[i] == 2 or l[-(i+1)] == 2:
        if l[i] == 2:
            sum += costs[l[-(i+1)]]
        else:
            sum += costs[l[i]]
    else:
        print(-1)
        exit()

print(sum)

C++ Code:

/** Read statement carefully **/

#include <bits/stdc++.h>
using namespace std;

#define     debug(x)            cout<< #x << " = " << x <<endl;
#define     ll                  long long
#define     yes                 cout<< "Yes" << "\n"
#define     no                  cout<< "No"  << "\n"
#define     eps                 1e-7
#define     pb                  push_back
#define     F                   first
#define     S                   second
#define     endl                "\n"

const int sz  = 2e5 + 5;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const ll  Inf = 1e18;
ll dr[] = {-1, +1, 0,   0};
ll dc[] = { 0,  0, +1, -1};


void solve() {
    ll n, aa, bb; cin>> n >> aa >> bb;
    ll ara[n];

    for(ll i = 0; i <n; i ++) cin>> ara[i];
    ll ans = 0;
    if(n % 2) {
        if(ara[n / 2] == 2) {
            ans = min(aa, bb);
        }
    }

    for(ll i = 0; i < n / 2; i ++) {
        if(ara[i] == ara[n - i - 1]) {
            if(ara[i] == 2)
                ans += 2 * min(aa, bb);
        }
        else {
            if((ara[i] ^ ara[n - i - 1]) == 1) {
                cout<< -1 << endl; return;
            }
            else {
                ll a;
                if(ara[i] ==  2) a = ara[n - i - 1];
                else a = ara[i];

                if(a == 0) ans += aa;
                else ans += bb;

            }
        }
    }

    cout<< ans << endl;
}


int32_t main()
{

    ios::sync_with_stdio(false);cin.tie(0);
    ll T; T = 1;
   // cin >> T;
    while(T --) {
        solve();

    }


    return 0;

}


Comments

Submit
0 Comments
More Questions

1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat